package fr.uvsq.isty.bdh.hchess.gui.game;

import fr.uvsq.isty.bdh.hchess.engine.GameData;
import fr.uvsq.isty.bdh.hchess.engine.PiecePromotionButton;
import fr.uvsq.isty.bdh.hchess.gui.style.Style;
import ictk.boardgame.chess.Bishop;
import ictk.boardgame.chess.Knight;
import ictk.boardgame.chess.Queen;
import ictk.boardgame.chess.Rook;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.io.IOException;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JToolBar;

/**
 * A JToolBar that allow to choose the Piece in wich your pawn will be promoted. 
 * @author Rémi Dubois <RemiEric.Dubois@gmail.com>
 */
public class PromotionChoicePanel extends JToolBar implements Observer {
    
    private Style style; /** Board style singleton */
    
    public PromotionChoicePanel(GameData gameData) {
        
        super("Choose your promotion");
        
        
        //setPreferredSize(new Dimension(BoardDisplay.DEFAULT_CASE_WIDTH*4, BoardDisplay.DEFAULT_CASE_HEIGHT));
        
        this.setLayout(new FlowLayout());
        
        try {
            this.style = Style.getInstance();
        } catch (IOException ex) {
        }
        
        PiecePromotionButton queenButton =
                new PiecePromotionButton(Queen.INDEX);
        PiecePromotionButton rookButton =
                new PiecePromotionButton(Rook.INDEX);
        PiecePromotionButton bishopButton =
                new PiecePromotionButton(Bishop.INDEX);
        PiecePromotionButton knightButton =
                new PiecePromotionButton(Knight.INDEX);

        this.add(queenButton);
        gameData.addObserver(queenButton);
        
        this.add(rookButton);
        gameData.addObserver(rookButton);

        this.add(bishopButton);
        gameData.addObserver(bishopButton);

        this.add(knightButton);
        gameData.addObserver(knightButton);
        
        this.setVisible(false);
    }
    
    @Override
    public void paintComponent(Graphics g) {
        Image bg = style.getBGImage();
        
        g.drawImage(bg, 0, 0, this.getWidth(), this.getHeight(), this);
    }
    
    /**
     * make the PromotionChoicePanel displayed only when ther is a promotion
     * @param o the observable (the GameData)
     * @param arg unused
     */
    public void update(Observable o, Object arg) {
        if(o instanceof GameData) {
            GameData gameData = (GameData) o;
            
            if(gameData.isPromoting()) {
                this.setVisible(true);
                this.repaint();
            }
            else {
                this.setVisible(false);
            }
        }
        
    }
}
